<?php

namespace app\modules\Finance\models;

use Yii;
use app\modules\Finance\models\InvoiceCompany;
use dektrium\user\models\User;


/**
 * This is the model class for table "invoice".
 *
 * @property integer $id
 * @property integer $finance_id
 * @property string $code
 * @property string $name
 * @property string $company
 * @property string $tax
 * @property string $bank_account
 * @property string $bank_name
 * @property string $address
 * @property string $phone
 * @property string $remark
 * @property integer $status
 * @property integer $user_id
 * @property string $created_at
 * @property string $updated_at
 */
class Invoice extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'invoice';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name'], 'required'],
            [['finance_id', 'status', 'user_id','name','type'], 'integer'],
            [['created_at', 'updated_at'], 'safe'],
            [['code'], 'string', 'max' => 20],
            [['company', 'tax', 'bank_account', 'bank_name', 'address', 'phone'], 'string', 'max' => 128],
            [['remark'], 'string', 'max' => 256],
        ];
    }

    // 保存前处理
    public function beforeSave($insert) {//{{{
        if($insert){
            $this->created_at = date("Y-m-d H:i:s");
            $this->user_id    = Yii::$app->user->id;
        }
        return parent::beforeSave($insert);
    }//}}}

    // 保存后执行
    public function afterSave($insert, $changedAttributes){//{{{

        parent::afterSave($insert, $changedAttributes);

        if($insert) {
            // 判断收款单位是否存在 不存在新增一条
            if($this->company) {
                $ret = InvoiceCompany::find()->where(['company'=>$this->company, 'tax'=>$this->tax])->one();

                if(empty($ret)){
                    $model = new InvoiceCompany();
                    $model->company  = $this->company;
                    $model->tax = $this->tax;
                    $model->bank_account = $this->bank_account;
                    $model->bank_name  = $this->bank_name;
                    $model->address  = $this->address;
                    $model->phone  = $this->phone;

                    $model->save();
                }else{ //更新
                    $ret->company  = $this->company;
                    $ret->tax = $this->tax;
                    $ret->bank_account = $this->bank_account;
                    $ret->bank_name  = $this->bank_name;
                    $ret->address  = $this->address;
                    $ret->phone  = $this->phone;

                    $ret->save();
                }
            }

            // 日志
            //$desc = sprintf("创建发票单  单号 %s", $this->code);
            $desc = "创建发票单";
            Yii::$app->logs->info($this->id, 9, $desc);
        }
    }//}}}

    //  获取对应的财务单
    public function getFinance(){
        return $this->hasOne(Finance::className(), ['id' => 'finance_id']); 
    }

    //  获取关联的订单总金额
    public function getOrderPrice(){
        return $this->hasMany(InvoiceOrder::className(), ['invoice_id' => 'id'])->sum('price');
         
    }

    //  获取关联的订单
    public function getInvoiceOrder(){
        
        return $this->hasMany(InvoiceOrder::className(), ['invoice_id' => 'id']);
    }

    //  获取审批负责销售信息
    public function getUser(){
        return $this->hasOne(User::className(), ['id' => 'user_id']);
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'finance_id' => Yii::t('app.invoice', 'Finance ID'),
            'code' => Yii::t('app.invoice', 'Code'),
            'name' => Yii::t('app.invoice', 'Name'),
            'type' => Yii::t('app.invoice', 'Type'),
            'company' => Yii::t('app.invoice', 'Company'),
            'tax' => Yii::t('app.invoice', 'Tax'),
            'bank_account' => Yii::t('app.invoice', 'Bank Account'),
            'bank_name' => Yii::t('app.invoice', 'Bank Name'),
            'address' => Yii::t('app.invoice', 'Address'),
            'phone' => Yii::t('app.invoice', 'Phone'),
            'remark' => Yii::t('app.invoice', 'Remark'),
            'status' => Yii::t('app', 'Status'),
            'user_id' => Yii::t('app', '提交人'),
            'created_at' => Yii::t('app', 'Created At'),
            'updated_at' => Yii::t('app', 'Updated At'),
        ];
    }
}
